package com.smartTerminal.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * Base64处理类
 * @project	smartTerminal
 * @author	Xia.Zhuming
 * @data	2018年1月10日
 */
public class Base64Util {

	/**
	 * 将base64格式的图片转换成本地图片文件
	 * @method	base64ToFileByte
	 * @author	Xia.Zhuming
	 * @data	2018年1月12日
	 * @param fileJson	base64字符串
	 * @param fileName	本地临时文件名(如1.png)
	 * @return	File		本地临时文件
	 * @throws Exception
	 */
	public static File base64ToFile(String fileJson, String fileName) throws Exception{
		String prvPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
		String path = prvPath.substring(1,prvPath.indexOf("classes")) + "temp/";
		String tempPath = path + fileName;
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		file = new File(tempPath);
		if(file.exists()){
			file.delete();
		}
		Base64 base64 = new Base64();
		byte[] b = base64.decode(fileJson);
		OutputStream out = new FileOutputStream(file);
		out.write(b);
		out.flush();
		out.close();
		return file;
	}
	
	/**
	 * 将指定信息写入base64字符串
	 * @method	toBase64
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param contents
	 * @param width
	 * @param height
	 * @return
	 * @throws Exception
	 */
	public static String toBase64(JSONObject obj, int width, int height) throws Exception {
		Hashtable<Object, Object> hints = new Hashtable<Object, Object>();   
        // 指定纠错等级   
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);   
        // 指定编码格式   
        BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(obj.toJSONString().getBytes("UTF-8"),"iso-8859-1"),BarcodeFormat.QR_CODE, width, height);  
        ByteArrayOutputStream bao = new ByteArrayOutputStream();  
        MatrixToImageWriter.writeToStream(byteMatrix, "png", bao);  
        String ok = Base64Code(bao.toByteArray());  
        bao.flush();
        bao.close();
		return ok;
	}
	/**
	 * base64加密
	 * @method	Base64Code
	 * @author	Xia.Zhuming
	 * @data	2018年1月10日
	 * @param b
	 * @return
	 */
	public static String  Base64Code(byte[] b) {  
        Base64 encoder = new Base64();  
        StringBuilder pictureBuffer = new StringBuilder();  
        pictureBuffer.append(encoder.encodeAsString(b));  
        String codeBase64 = pictureBuffer.toString();  
        return codeBase64;  
    }
	
	public static void main(String[] args) {
		try {
			JSONObject container1 = new JSONObject();
		    container1.put("QQ", "2418741378");
		    container1.put("昵称", "路人甲");
			String aa = Base64Util.toBase64(container1, 150, 150);
			System.out.println(aa);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
